package com.jmc.calls;
/////////////////////////////////////////////////////////////////////////
//
// Example that shows listing all spooled files on a server synchronously.
// Listing synchronously does not return to the caller until the complete list
// is built. The user perceives a slower response time then listing asynchronously.
//
/////////////////////////////////////////////////////////////////////////
//
// This source is an example of IBM Toolbox for Java "PrintObjectList".
//
/////////////////////////////////////////////////////////////////////////

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400Exception;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.RequestNotSupportedException;
import com.ibm.as400.access.SpooledFileList;
import com.ibm.as400.access.SpooledFile;

public class ListSpool
{
    private AS400 system_ = null;

    public ListSpool(AS400 system)
    {
        system_ = system;
    }

    public ListSpool() {
		// TODO Auto-generated constructor stub
	}

	public void listSpooledFiles()
    {
        try{
            String strSpooledFileName;

            if( system_ == null )
            {
                system_ = new AS400("www.transfurlong.com.ar", "qsecofer", "c2259");
            }

            System.out.println(" Now receiving all spooled files Synchronously");

            SpooledFileList splfList = new SpooledFileList( system_ );

            // set filters, all users, on all queues
            //splfList.setUserFilter("*ALL");
            splfList.setQueueFilter("/QSYS.LIB/%ALL%.LIB/%ALL%.OUTQ");

            // open list, openSynchronously() returns when the list is completed.
            splfList.setUserFilter("QSECOFER");
            splfList.openSynchronously();
            
            Enumeration enumeration = splfList.getObjects();

            while( enumeration.hasMoreElements() )
            {
                SpooledFile splf = (SpooledFile)enumeration.nextElement();
                if ( splf != null )
                {
                    // output this spooled file's name
                    strSpooledFileName = splf.getStringAttribute(SpooledFile.ATTR_SPOOLFILE);
                    
                    
                    System.out.print(" spooled file = " + strSpooledFileName);
                    System.out.print(" spooled file = " + splf.getJobName());
                    System.out.print(" spooled file = " + splf.getJobNumber());
                    System.out.print(" spooled file = " + splf.getJobUser());
                    System.out.println(" spooled file = " + splf.getNumber());
                    System.out.println(" date file = " + splf.getStringAttribute(SpooledFile.ATTR_DATE));
                    System.out.println(" date file = " + splf.getStringAttribute(SpooledFile.ATTR_TIME));
                    


                }
                
            }
            // clean up after we are done with the list
            splfList.close();
        }
        catch( Exception e )
        {
            // ...handle any exceptions...
            e.printStackTrace();
        }
    }

    public void listar(){
    	ListSpool list = new ListSpool(new AS400("www.transfurlong.com.ar", "qsecofer", "c2259"));
        try{
            list.listSpooledFiles();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        System.exit(0);
    	
    }
    public static void main( String args[] )
    {
    	ListSpool list = new ListSpool(new AS400("www.transfurlong.com.ar", "qsecofer", "c2259"));
        try{
            list.listSpooledFiles();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        System.exit(0);
    }
}